#include "PlasmaCannonStates.hpp"
#include "AnimatedSprite.hpp"
#include "PlasmaCannon.hpp"
#include "../KeysDefinitions.hpp"
#include "../MMessage.hpp"
#include "../SceneManager.hpp"
#include "../InputManager.hpp"
#include <iostream>

using namespace std;

//! PLASMA_CANNON_LISTEN
void PlasmaCannonListen::beginState(PlasmaCannon *)
{
}

void PlasmaCannonListen::updateState(PlasmaCannon *, clock_t)
{
}

void PlasmaCannonListen::endState(PlasmaCannon *)
{
}

void PlasmaCannonListen::handleMessage(PlasmaCannon *pCannon, MMessage *pMsg)
{
	switch(pMsg->getCommand())
	{
	case MSG_CMD_KEYPRESS:
		int key = *(static_cast<int*>(pMsg->getAddInfo()));
		
		switch(key)
		{
		case KEY_SPACE:
			if(pCannon->getStateMachine()->getCurrentState()->getStateName() != PCSN_SHOT)
				pCannon->getStateMachine()->changeState(new PlasmaCannonShot());
			break;
		}
		break;
	}
}


//! PLASMA_CANNON_IDLE
void PlasmaCannonIdle::beginState(PlasmaCannon *)
{
}

void PlasmaCannonIdle::updateState(PlasmaCannon *, clock_t)
{
}

void PlasmaCannonIdle::endState(PlasmaCannon *)
{
}

//! PLASMA_CANNON_SHOT
void PlasmaCannonShot::beginState(PlasmaCannon* pCannon)
{
	float fScale = 0.5;
	MVector3D pos = pCannon->getShotStartPos();
	MVector3D offset = pCannon->getOffset();
	m_fTempOffset = fScale * 0.5f;
	offset.z -= m_fTempOffset;
	pos = pos + offset;
	pCannon->setOffset(offset);
	m_pGunfire = new AnimatedSprite("plasma_shot.png", 1, 250, pos, false, fScale, fScale);
	SceneManager::GetInstance()->RegisterEntity(m_pGunfire);

	//pos.z -= 0.5f;
	//m_pBullets = new AnimatedSprite("shots.png", 4, 50, pos, true, 0.25);
	//SceneManager::GetInstance()->RegisterEntity(m_pBullets);

	m_iCooldown = pCannon->getFireRate();

	//m_pVec = new vector<int>();
	//SceneManager::GetInstance()->getDynamicIDS(m_pVec);
}

void PlasmaCannonShot::updateState(PlasmaCannon* pCannon, clock_t tick)
{
	MVector3D pos = pCannon->getShotStartPos();
	pos = pos + pCannon->getOffset();//0.9f;
	m_pGunfire->setPosition(pos);

	if(InputManager::getInstance()->lookUpKeyStatus(KEY_SPACE))// == KEY_RELEASED && m_pGunfire->isOver() && m_iCooldown <= 0)
		pCannon->getStateMachine()->changeState(new PlasmaCannonIdle());


	if(m_iCooldown == pCannon->getFireRate())
	{
		if(!m_pGunfire->isOver())
			return;

		m_pGunfire->restart();
		/*vector<int>::iterator currElement = (*m_pVec).begin();
		vector<int>::iterator endElement  = (*m_pVec).end();

		for(currElement; currElement != endElement; ++currElement)
		{
			Entity3D* pHostile = static_cast<Entity3D*>(SceneManager::GetInstance()->GetEntityByID((*currElement)));
			pEntity->shotAutocannon(pHostile);
		}*/
	}

	m_iCooldown -= tick;///1000.0f;
	if(m_iCooldown <= 0)
		m_iCooldown = pCannon->getFireRate();


	//m_pGunfire->translate(MVector3D(0.0, 0.0, -0.015));
	//m_pBullets->translate(MVector3D(0.0, 0.0, -0.015));
}

void PlasmaCannonShot::endState(PlasmaCannon* pCannon)
{
	MVector3D offset = pCannon->getOffset();
	offset.z += m_fTempOffset;
	pCannon->setOffset(offset);
	//delete m_pVec;

	SceneManager::GetInstance()->UnregisterEntity(m_pGunfire->getEntityID());
	//SceneManager::GetInstance()->UnregisterEntity(m_pBullets->getEntityID());
}

//EOF
